<!-- 派单 -->
<style>
    .js-order-send-to-agency-confirm {
        position: fixed;
        width: 400px;
        height: 200px;
        top: 50%;
        left: 50%;
        background-color: #ffffff;
        margin: -200px 0 0 -200px;
        z-index: 19999;
    }

    .js-order-send-to-agency-confirm form {
        padding: 20px 0 25px;
    }

    .js-order-send-to-agency-confirm select,
    .js-order-send-to-agency-confirm input {
        width: 100%;
        box-sizing: border-box;
        padding: 16px 6px;
    }

    .js-order-send-to-agency-confirm .line {
        padding-bottom: 5px;
        margin-bottom: 10px;
        border-bottom: 1px solid #e6e6e6;
    }
</style>
<div class="float-confirm js-order-send-to-agency-confirm" style="display: none;">
    <div class="float-confirm-con">
        <h2 class="line" style="font-size:18px;color:#23a18b;">委派订单 (至指定合伙人)</h2>
        <form>
            <input type="hidden" name="order_id">
            <select placeholder="请选择服务合伙人" class="add-f-ipt1 js-service-agency-id" name="service_agency_id">
            </select>
        </form>
        <div class="line"></div>
        <div class="float-confirm-btns" style="padding-bottom:30px;">
            <a class="btn btn-default btn-sm right js-cancel" href="javascript:void(0);">取消</a>
            <a class="btn btn-success btn-sm right js-submit" href="javascript:void(0);">确定</a>
        </div>
    </div>
</div>
{load_css file='js/jquery/jquery-editable-select.min.css'}
{load_js file='jquery/jquery-editable-select.js'}
<script>
    (function ($) {

        var __AGENCY_LIST;

        var layer = $('.js-order-send-to-agency-confirm');
        var suggestion;

        var formEl = layer.find('form');
        var selectEl = formEl.find('select');

        var agency_id;
        var order_id;
        var original_agency_id;

        var options = {
            effects: 'slide',
            additional: true
        };


        if (__AGENCY_LIST == null) {
            getAgencyList(function () {
                createSelect();
                selectEl.editableSelect(options);
                suggestion = formEl.find('.js-service-agency-id').data('editable-select');
            });
        }

        function getAgencyList(cb) {
            $.get('{AnUrl("order/admin/store_order/ajax_get_service_dealers_list")}', function (ret) {
                if (ret.flag == 0) {
                    __AGENCY_LIST = ret.response;
                }
                cb && cb.call && cb();
            });
        }

        function createSelect() {
            selectEl.html(function () {
                var els = [];
                els.push('<option value="0">请选择服务合伙人</option>');
                for (var i = 0, len = __AGENCY_LIST.length; i < len; i++) {
                    els.push('<option value="' + __AGENCY_LIST[i].id + '">' + __AGENCY_LIST[i].realname + '(' +
                            __AGENCY_LIST[i].mobile +
                            ')' + '</option>'
                    )
                }
                return els.join('');
            });
        }

        $(document.body).on('click', '.js-set-service-agency', function (e) {
            e.preventDefault();

            var el = $(this);
            order_id = el.data('order_id');
            formEl.find('[name="order_id"]').val(order_id);
            agency_id = el.data('agency_id');
            original_agency_id = el.data('original_agency_id');

            var li;
            if (parseInt(original_agency_id) > 0) {
                li = suggestion.$list.find('[value="' + agency_id + '"]');
                suggestion.select(li);
            } else {
                li = suggestion.$list.find(':first-child');
            }
            suggestion.select(li);

            layer.show();
            $.pageMask.show();
            return false;
        });

        layer.on('click', '.js-submit', function (e) {
            e.preventDefault();

            var _d = formEl.serializeArray();
            var data = {};
            for (var i = 0; i < _d.length; i++) {
                data[_d[i].name] = _d[i].value;
            }
            if (!data.service_agency_id || data.service_agency_id == '0') {
                showMsg('请选择合伙人');
            } else if (data.service_agency_id == agency_id) {
                showMsg('委派合伙人不可与原始合伙人相同');
            } else {
                showConfirm('将此订单委派给合伙人<br/>' + suggestion.$input.val() + ' ?', 0, function () {
                    $.post('{AnUrl("order/admin/store_order/ajax_set_service_dealers")}', data, function (ret) {
                        if (ret) {
                            if (ret.flag == 0) {
                                showMsg(ret.message);
                                setTimeout(function () {
                                    window.location.reload();
                                }, 1500);
                                return;
                            }
                            showMsg(ret.message);
                            return;
                        }
                        showMsg('操作失败');
                    });
                });
            }
            return false;
        });

        layer.on('click', '.js-cancel', function (e) {
            e.preventDefault();
            layer.hide();
            $.pageMask.hide();
            suggestion.select(suggestion.$list.find(':first-child'));
            return false;
        });

    })(jQuery);
</script>
<!-- /派单 -->